<?php
/**
 * @name /www/EditUser.php
 * @author
 * @version 2011.03.30
 *
 * This is the page for Editing User Information by the Administrator.
 */

// Sets up configuration settings and includes libraries.
require_once('_ini.php');

// Libraries
ReqLib('/lib/private/User.php');


function DefineHeader() {
?><script type="text/javascript">
  function onModifyRoles(select, value, text) {
    $roles = $('#RolesSelect Option');
    var text = '';
    $.each($roles, function(i, role) {
      text += (i ? '|' : '') + role.value;
    });
    $('#RolesHidden').val(text);
  }

  $(function() {
    $('#RolesSelect').selectList({
      onAdd: onModifyRoles,
      onRemove: onModifyRoles
    });
  });
</script>
<?
}

/**
 * Defines page content.
 *
 */
function DefineContent() {
  // Page content goes here.
  ?>
<p>
  <a href="<?=_webroot('/') ?>">Home</a>.
</p>
<?
	if($_SESSION['userId'] >0){
?>
		<p>
		  <a href="<?=_webroot('/account.php') ?>">My Account</a>.
		</p>
		<p>
		  <a href="<?=_webroot('/logout.php') ?>">Logout</a>.
		</p>
    <form  action = "userRequestHandle.php" method="POST">
<center><table border=2>
	<tbody>
<?
	$u = new User();
	$userId = $_SESSION['userId'];

	//print($userId);/

	$u->Load($userId);

	//print_r($u);

	print('<input type="hidden" name="UserId" value="'.$u->UserId.'" />');
	print('<tr><td>First Name</td><td><input type="text" name="FirstName" value="'.$u->FirstName.'" /></td></tr>');
	print('<tr><td>Last Name</td><td><input type="text" name="LastName" value="'.$u->LastName.'" /></td></tr>');
	print('<tr><td>Phone Number</td><td><input type="text" name="PhoneNumber" value="'.$u->PhoneNumber.'" /></td></tr>');
	print('<tr><td>Email</td><td><input type="text" name="Email" value="'.$u->Email.'" /></td></tr>');
	print('<tr><td>Street 1</td><td><input type="text" name="Street1" value="'.$u->Street1.'" /></td></tr>');
	print('<tr><td>Street 2</td><td><input type="text" name="Street2" value="'.$u->Street2.'" /></td></tr>');
	print('<tr><td>Street 3</td><td><input type="text" name="Street3" value="'.$u->Street3.'" /></td></tr>');
	print('<tr><td>City</td><td><input type="text" name="City" value="'.$u->City.'" /></td></tr>');
	print('<tr><td>State</td><td><input type="text" name="State" value="'.$u->State.'" /></td></tr>');
	print('<tr><td>Country</td><td><input type="text" name="Country" value="'.$u->Country.'" /></td></tr>');
	print('<tr><td>Postal Code</td><td><input type="text" name="PostalCode" value="'.$u->PostalCode.'" /></td></tr>');
	print('<tr><td>News Letter Subscription</td><td><input type="checkbox" name=NewsletterSubscription value="yes"');

	if($u->NewsletterSubscription == 0)
		print(' /></td></tr>');
	else
		print(' checked /></td></tr>');

  // Get all roles as an array
  $rolesOptions = User::GetRoles();
  array_push($rolesOptions, 'Select Role...');
  // For each role in the array, wrap it with HTML option markup
  for ($i = 0; $i < count($rolesOptions); $i++) {
    $o = $rolesOptions[$i];
	if($o == "Administrator" && !($u->HasRole("Administrator")))
		continue;
    $o = '<option value="' . htmlspecialchars($o) . '"' . ($u->HasRole($o) ? ' selected="selected"' : '') . '>' . htmlspecialchars($o) . '</option>';
    $rolesOptions[$i] = $o;
  }
  // Turn the array into a string
  $rolesOptions = implode('', $rolesOptions);
  // Split out the roles as an HTML select element
	print('<tr><td>Roles</td><td><select id="RolesSelect" multiple="multiple" title="Select Role...">' . $rolesOptions . '</select></td></tr>');
	print('</tbody></table>');
  	print('<input type="submit" name="Action" value="Update My Info" />');
	print('<input type=button value="Back" onClick="history.go(-1)" />');
	print('</center>');
	print('<input type="hidden" name="Roles" id="RolesHidden" value="' . implode('|', $u->_Roles) . '" />');

  	print('</form>');
	}
   else{
?>
		<p>
		  <a href="<?=_webroot('/login_register.php') ?>">Login/Register</a>.
		</p>
<?
   }
}

// Launches the page; without this, nothing happens.
require_once('_loader.php');